package com.hh.common.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMqConfig {

    //订单消息交换机
    public static final String ORDER_MESSAGE_EXCHANGE = "order_message_exchange";

    //IM交换机
    public static final String IM_EXCHANGE = "im_exchange";

    //延时交换机
    public static final String DELAYED_MESSAGE_EXCHANGE = "delayed_message_exchange";

    //订单支付成功后续处理队列
    public static final String ORDER_PAY_SUCCESS_AFTER_QUEUE = "order_pay_success_after_queue";

    //im用户注册处理队列
    public static final String IM_USER_REGISTER_QUEUE = "im_user_register_queue";

    //订单未支付延时取消队列
    public static final String ORDER_DELAYED_CANCEL_QUEUE = "order_delayed_cancel_queue";

    /**
     * 多例的RabbitTemplate
     * @param connectionFactory
     * @return
     */
    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory){
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setMandatory(true);
        template.setMessageConverter(new Jackson2JsonMessageConverter());
        return template;
    }

    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }


    /****************************创建RabbitMq交换机、队列，绑定交换机和队列**************************/

    @Bean
    public Exchange bootOrderExchange(){
        return ExchangeBuilder.directExchange(ORDER_MESSAGE_EXCHANGE).durable(true).build();
    }

    @Bean
    public Queue bootOrderPaySuccessQueue(){
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-queue-type","classic");
        return QueueBuilder.durable(ORDER_PAY_SUCCESS_AFTER_QUEUE).withArguments(arguments).build();
    }

    @Bean
    public Binding bootOrderBinding(Exchange bootOrderExchange,Queue bootOrderPaySuccessQueue){
        return BindingBuilder.bind(bootOrderPaySuccessQueue).to(bootOrderExchange).with(ORDER_PAY_SUCCESS_AFTER_QUEUE).noargs();
    }





    @Bean
    public Exchange bootImExchange(){
        return ExchangeBuilder.directExchange(IM_EXCHANGE).durable(true).build();
    }

    @Bean
    public Queue bootImUserRegisterQueue(){
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-queue-type","classic");
        return QueueBuilder.durable(IM_USER_REGISTER_QUEUE).withArguments(arguments).build();
    }

    @Bean
    public Binding bootImBinding(Exchange bootImExchange,Queue bootImUserRegisterQueue){
        return BindingBuilder.bind(bootImUserRegisterQueue).to(bootImExchange).with(IM_USER_REGISTER_QUEUE).noargs();
    }




    @Bean
    public Exchange bootDelayedExchange(){
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-delayed-type","direct");
        Exchange exchange = new CustomExchange(DELAYED_MESSAGE_EXCHANGE, "x-delayed-message", true, false, arguments);
        return exchange;
    }

    @Bean
    public Queue bootDelayedQueue(){
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-queue-type","classic");
        return QueueBuilder.durable(ORDER_DELAYED_CANCEL_QUEUE).withArguments(arguments).build();
    }
    @Bean
    public Binding bootDelayedBinding(Queue bootDelayedQueue,Exchange bootDelayedExchange){
        return BindingBuilder.bind(bootDelayedQueue).to(bootDelayedExchange).with(ORDER_DELAYED_CANCEL_QUEUE).noargs();
    }
}
